//
//  MainTableViewController.swift
//  ZMWB
//
//  Created by 朱敏 on 2017/9/19.
//  Copyright © 2017年 朱敏. All rights reserved.
//

import UIKit

class MainViewController: UITabBarController {

    
    // MARK: - 生命周期方法
    override func viewDidLoad() {
        super.viewDidLoad()
        
        tabBar.tintColor = UIColor.orange
        
        addChildViewControllers()
        
        
    }
    
    override func viewWillAppear(_ animated: Bool) {
        super.viewWillAppear(animated)
        
        // 1.添加加号按钮
        tabBar.addSubview(composeButton)
        
        // 2.设置加号按钮的位置
        let rect = composeButton.frame.size
        // 计算宽度
        let width = tabBar.bounds.width / CGFloat(childViewControllers.count)

        composeButton.frame = CGRect(x: 2 * width, y: 0, width: width, height: rect.height)
        ZMLog(message: tabBar.center)
    }
    
    // MARK: - 内部控制方法
    // 添加所有子控制器
    func addChildViewControllers() {

        guard let jsonPath = Bundle.main.path(forResource: "MainVCSettings.json", ofType: nil) else {
            ZMLog(message: "没有获取到对应的文件路径")
            return
        }
        //2.读取json文件中的内容
        guard let jsonData = try? Data(contentsOf: URL(fileURLWithPath: jsonPath)) else {
            ZMLog(message: "没有获取到json文件中数据")
            return
        }
        //3.将Data转成数组
        guard  let dictArray = try? JSONSerialization.jsonObject(with: jsonData, options: .mutableContainers) else {
            addChildViewController("HomeTableViewController", title: "首页", imageName: "tabbar_home")
            addChildViewController("MessageTableViewController", title: "消息", imageName: "tabbar_message_center")
            addChildViewController("NullViewController", title: "", imageName: "")
            addChildViewController("DiscoverTableViewController", title: "发现", imageName: "tabbar_discover")
            addChildViewController("ProfileTableViewController", title: "我", imageName: "tabbar_profile")
            return
        }
        
        //4.遍历字典,获取对应的信息
        for dict in dictArray as! [[String:String]]{
            //4.4添加子控制器
            addChildViewController(dict["vcName"]!, title: dict["title"]!, imageName: dict["imageName"]!)
        }
    }
    
    
    // override代表重写父类的方法
    // Swift支持方法的重载， 也就是说只要方法的参数个数或者数据类型不相同，那么系统就会认为是两个方法
    func addChildViewController(_ childVcName: String,title: String, imageName: String) {
        
        //0.获得命名空间
        let nameSpace = (Bundle.main.infoDictionary!["CFBundleExecutable"] as! String)
        //1.根据字符创获取对应的Class
        let ChildVcClass:AnyClass = NSClassFromString(nameSpace + "." + childVcName)!
        // 2.将对应的AnyObject转成控制器的类型
        let childVcType = ChildVcClass as! UITableViewController.Type
        // 3.创建对应的控制器对象
        let childVc = childVcType.init()
        
        //4.设置自控制器的属性
        childVc.title = title
        childVc.tabBarItem.image = UIImage(named: imageName)
        childVc.tabBarItem.selectedImage = UIImage(named: imageName + "_highlighted")
        //5.包装导航控制器
        let childNav = UINavigationController(rootViewController: childVc)
        //6.添加控制器
        addChildViewController(childNav)

    }
    
    // 加上 @objc就代表告诉系统需要动态派发
    @objc private func composeBtnClick(btn: UIButton) {
        ZMLog(message: "+")
    }
    
    // MARK: - 懒加载
    lazy var composeButton: UIButton = {
        () -> UIButton
        in
        // 1.创建按钮
        let btn = UIButton(image:"tabbar_compose_icon_add", backgroundImage:"tabbar_compose_button")
        
        btn.addTarget(self, action: #selector(MainViewController.composeBtnClick(btn:)), for: UIControlEvents.touchUpInside)
        
        return btn
    }()
}
